package com.springbootdemo.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RedisConfig {
    /**
     * 定制redisTemplet 作用 存储到redis中的对象以序列化的方式
     *
     * @param redisConnectionFactory
     * @return
     */
//    @Bean
//    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }

    /**
     * redis缓存序列化
     *
     * @param redisConnectionFactory
     * @return
     */
//   解决 注解的方式系列化的
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//        String系列化方式
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
//        json系列化方式
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        系列化默认配置
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
//系列化配置修改
        RedisCacheConfiguration config2 = config
                //        设置key为string系列化
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
                //        设置value为json系列化
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
//                不缓存null
                .disableCachingNullValues()
//                设置过期时间30分钟
                .entryTtl(Duration.ofMinutes(30L));
        Map<String, RedisCacheConfiguration> map = new HashMap<>();
//        设置缓存区域为oneUser的过期时间
        map.put("oneUser", config2.entryTtl(Duration.ofMinutes(3L)));
        map.put("allUser", config2.entryTtl(Duration.ofMinutes(30L)));
        RedisCacheManager redisCacheManager = RedisCacheManager.builder(redisConnectionFactory)
                .cacheDefaults(config2)
                //特殊缓存，设置缓存区域为oneUser的过期时间
                .withInitialCacheConfigurations(map)
//                事务
                .transactionAware()
                .build();
        return redisCacheManager;
    }
//解决RedisTemplate方式的乱码
    @Bean
    public RedisTemplate setRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
//        String系列化方式
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
//        json系列化方式
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(redisSerializer);
        redisTemplate.setValueSerializer(redisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
    }

}